function ListNode(val, next) {
    this.val = (val === undefined ? 0 : val)
    this.next = (next === undefined ? null : next)
}
/**
 * 转换为数组 操作 在赋值给 链表
 * @param {*} head 
 * @param {*} k 
 * @returns 
 */
// var rotateRight = function (head, k) {
//     let arr = [],
//         n = 0,
//         res = [];
//     let curr = head;
//     let midHead = new ListNode(0,head)
//     let d = midHead
//     while (curr) {
//         arr[n++] = curr.val
//         curr = curr.next
//     }
//     k = k % n
//     for (let i = 0; i < n; i++) {
//         res[(i+k)%n] = arr[i ]
//     }
//     let i=0
//     while(d.next&&d){
//         d.next.val=res[i++]
//         d=d.next
//     }
//     return midHead.next
// };

/**
 * 常规法 
 * 将链表旋转k次   其实就是将虚拟表头指向倒数第k个节点
 * 在首尾相连 将表头变成环   在断开 指向新的表头的节点 
 * @param {*} head 
 * @param {*} k 
 * @returns 
 */
var rotateRight = function (head, k) {
    if(!head) return null
    let h = head
    let dHead = new ListNode(0,head);
    let current = dHead;
    let n = -1;
    while(current){
        //记录链表的长度
        //闭合成环
        if(!current.next){
            current.next=head
            break
        }
        n++
        current=current.next
    }
    k=k%n
    let i = 0
    while(n-k-i>0){
        dHead.next=dHead.next.next
        i++
    }
    i=1
    while(n-k-i>0){
        h=h.next
        i++
    }
    h.next=null
    return dHead.next
}